﻿using System.Collections.Generic;
using System.Xml;
using UnityEngine;

namespace Sliver.Roadnet.OpenDrive
{
    public class OpenDriveRoadnet
    {
        protected readonly List<RawRoadNode> RawRoadNodes = new List<RawRoadNode>();
        protected readonly List<RawJunctionNode> RawJunctionNodes = new List<RawJunctionNode>();

        protected float WorldOffset = 0;

        public OpenDriveRoadnet()
        {

        }

        public void Init(XmlDocument document)
        {

            XmlElement root = null;
            foreach (object o in document.ChildNodes)
            {
                if (o is XmlElement e)
                {
                    root = e;
                    Debug.Log($"root: {root.Name}");
                }
            }

            if (root == null) return;

            PrepareToLoad(root);
            CalculateWorldOffset();
        }

        protected void PrepareToLoad(XmlElement root)
        {
            foreach (XmlNode node in root.ChildNodes)
            {
                switch (node.Name)
                {
                    case "header":
                        // Debug.Log(node.OuterXml);
                        break;
                    case "road":
                        RawRoadNodes.Add(new RawRoadNode(node));
                        break;
                    case "junction":
                        RawJunctionNodes.Add(new RawJunctionNode(node));
                        break;
                }
            }
        }

        protected void CalculateWorldOffset()
        {
            foreach (RawRoadNode roadNode in RawRoadNodes)
            {
                
            }
        }
    }
}
